App.module.UserRole.Role.Add = Ext.extend(Ext.util.Observable, {
	init: function(tree){
		this.tree = tree;
		tree.on('render', function(tree){
			var bt = new Ext.Button({
				tooltip: 'Add'
				,iconCls: 'icon-add-16x16'
				,disabled: true
				,handler: this.addNode
				,scope: this
			}, this);
			
			tree.getTopToolbar().add(bt);
			tree.getSelectionModel().on('selectionchange', function(sm, node){
				bt.setDisabled(!node);	
			});
		}, this);	
	}
	,addNode: function(br){
		var selected = this.tree.getSelectionModel().getSelectedNode();
		if(!selected) return;
		
		// add a new role
		var data = {
			parentId: selected.id
			,name: 'New_role'		
		};
		
		this.tree.el.mask('Processing...');
		
		API.role.add(data, function(r){
			this.tree.el.unmask();
			
			if(!r.success) return Ext.Msg.alert('Error', r.msg);
			
			var node = new Ext.tree.TreeNode({
				id: r.id
				,text: 'New_role'
				,expanded: true
				,children: []
			});
			selected.appendChild(node).select();
			this.tree.editor.triggerEdit(node);
		}, this);
		
		
	}
});